package com.kitten.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.builders.InMemoryClientDetailsServiceBuilder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;


@Order(Integer.MIN_VALUE)
@Configuration
@EnableAuthorizationServer
public class KittenAuthorizationServerConfig extends  AuthorizationServerConfigurerAdapter {

   @Autowired
   private  UserDetailsService userDetailsService;


    /**
     * test
     * 为了使用“password”授权方式，我们需要通过spring的@Autowired注解来注入和使用AuthenticationManager bean
     * @return
     */
    @Autowired
    private  AuthenticationManager authenticationManager;



    @Autowired
    private RedisConnectionFactory redisConnectionFactory;


    @Autowired
    private TokenStore tokenStore;


    /**
     * 认证及token配置
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(tokenStore)
                .authenticationManager(authenticationManager)
                .userDetailsService(userDetailsService);//配置userService 这样每次认证的时候会去检验用户是否锁定，有效等
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        InMemoryClientDetailsServiceBuilder builder =clients.inMemory();
        builder.withClient("kitten")//客户端ID
                .secret("kittensecret")
                .authorizedGrantTypes("refresh_token", "authorization_code", "password")//设置验证方式
                .accessTokenValiditySeconds(7200)//token过期时间
                .refreshTokenValiditySeconds(2592000)//refresh过期时间
                .scopes("all");
    }


    /**
     * tokenKey的访问权限表达式配置
     */
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security
                .allowFormAuthenticationForClients()
                // 开启/oauth/check_token验证端口认证权限访问
                .tokenKeyAccess("isAuthenticated()")
                // 开启/oauth/token_key验证端口无权限访问
                .checkTokenAccess("permitAll()");
    }


    /**
     * @return
     */
    @Bean
    public TokenStore redisTokenStore() {
        return new RedisTokenStore(redisConnectionFactory);
    }
}
